package org.chen.hotel.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.export.resquest.ExportRequest;
import org.chen.common.hotel.request.HotelSearchRequest;
import org.chen.common.result.ResultVO;
import org.chen.hotel.service.ExcelHotelExportService;
import org.chen.hotel.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Tag(name = "不重要的接口", description = "不重要的接口")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    @Autowired
    private ExcelHotelExportService excelHotelExportService;

    @PostMapping("/search")
    @Tag(name = "酒店查询的接口")
    public ResultVO searchHotels(@RequestBody HotelSearchRequest request) {
        return ResultVO.success(hotelService.searchHotels(request));
    }

    @PostMapping("/export")
    public ResponseEntity<byte[]> exportHotels(@RequestBody ExportRequest request) {
        try {
            byte[] excelData = excelHotelExportService.exportHotelsToExcel(request);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "hotels_" + System.currentTimeMillis() + ".xlsx");

            return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}